package com.cci.kangdao.fieldService.service.impl;

import com.cci.kangdao.dao.model.UserT;
import com.cci.kangdao.fieldService.dao.FieldServiceChannelDao;
import com.cci.kangdao.fieldService.dao.FieldServiceEngineerDao;
import com.cci.kangdao.fieldService.model.FieldServiceChannel;
import com.cci.kangdao.fieldService.model.FieldServiceEngineer;
import com.cci.kangdao.fieldService.service.FieldServiceChannelService;
import com.cci.kangdao.utilTool.CollectionUtils;
import com.cci.kangdao.utilTool.NumberUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author William Du
 * @date 2020/2/6
 */
@Service
public class FieldServiceChannelServiceImpl implements FieldServiceChannelService {

    private static final Logger logger = Logger.getLogger(FieldServiceChannelService.class);

    @Resource
    private FieldServiceChannelDao fieldServiceChannelDao;

    @Resource
    private FieldServiceEngineerDao fieldServiceEngineerDao;

    @Override
    public Long getChannel(UserT userT) {
        // 获取到渠道列表
        List<FieldServiceChannel> channelList = fieldServiceChannelDao.selectEffectiveChannels();
        if (CollectionUtils.isNotEmpty(channelList)) {
            for (FieldServiceChannel channel : channelList) {
                // 用户组是否匹配
                Boolean userGroupMatch = Boolean.FALSE;
                // 子用户组是否匹配
                Boolean subUserGroupMatch = Boolean.FALSE;
                // 企业是否匹配
                Boolean companyMatch = Boolean.FALSE;
                //查询用户组是否匹配
                if (channel.getUserGroupId().equals(userT.getUsergroupId())) {
                    userGroupMatch = Boolean.TRUE;
                    // 看渠道是否配置了子用户组值，若配置了则比较，未配置则默认通过
                    if (NumberUtils.isPositiveNum(channel.getSubUserGroupId())) {
                        if (channel.getSubUserGroupId().equals(userT.getUsergroupSubId())) {
                            subUserGroupMatch = Boolean.TRUE;
                        }
                    } else {
                        subUserGroupMatch = Boolean.TRUE;
                    }
                }
                //查询是否配置了企业，如果配置了则比较，未配置默认通过
                if (NumberUtils.isPositiveNum(channel.getCompanyId())) {
                    if (channel.getCompanyId().equals(userT.getCompanyId())) {
                        companyMatch = Boolean.TRUE;
                    }
                } else {
                    companyMatch = Boolean.TRUE;
                }
                // 当三者皆一致
                if (userGroupMatch && subUserGroupMatch && companyMatch) {
                    Long channelId = channel.getId();
                    // 判断是否存在WWID，此为服务工单工程师主要参数之一
                    String employeeNum = userT.getEmployeeNum();
                    if(StringUtils.isNotEmpty(employeeNum)){
                        FieldServiceEngineer engineer = fieldServiceEngineerDao.selectEngineer(channelId,userT.getEmployeeNum());
                        if(null == engineer){
                            logger.info("APP系统用户角色无误，CRM系统暂无用户信息，不允许进行服务");
                            return null;
                        }else {
                            return channelId;
                        }
                    }else{
                        logger.info("APP系统用户用户组角色对应，但缺少WWID，不允许进行服务");
                        return null;
                    }
                }
            }
        }
        return null;
    }

    @Override
    public Long getChannel(String channelNo) {
        if (StringUtils.isNotEmpty(channelNo)) {
            // 通过渠道编号查询
            FieldServiceChannel channel = fieldServiceChannelDao.selectByChannelNo(channelNo);
            if (null != channel) {
                return channel.getId();
            }
        }
        return null;
    }
}
